﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TopApp.Library
{
    public class Memo
    {
        public string salesman;
        public List<Lagniappe> lagniappes;
        public string text;

        #region 构造器
        public Memo( string memo )
        {
            if (memo == null) memo = "";

            this.salesman = "";
            this.text = "";
            this.lagniappes = new List<Lagniappe>();
            
            string[] temp = memo.Trim().Split(new char[] { ';' });

            if (temp.Length > 0 && (temp[0].Trim() != "" || temp[0] == null))
            {
                if (temp.Length == 1 && !this.isNumberic(temp[0].Trim()))
                {
                    this.text = temp[0].Trim();
                }
                else
                {
                    this.salesman = Convert.ToString(temp[0]);
                }
            }
            
            if (temp.Length > 1 && (temp[1].Trim() != "" || temp[1] == null))
            {
                string[] temp_lagniappes = temp[1].Split(new Char[] { ',' });
                foreach (string lagniappe in temp_lagniappes)
                {
                    if (lagniappe.Trim() != "")
                    {
                        this.lagniappes.Add(new Lagniappe(lagniappe));
                    }
                }
            }

            if (temp.Length > 2 && (temp[2].Trim() != "" || temp[2] == null)) this.text = temp[2];

        }
        #endregion

        #region 检查字条串是否是数字
        protected bool isNumberic(string message)
        {

            //判断是否为整数字符串

            int result = 0;

            try
            {
                result = Convert.ToInt32(message);

                return true;

            }

            catch
            {

                return false;

            }

        }
        #endregion

        #region 子类 Lagniappe
        public class Lagniappe
        {
            public string code;
            public int quantity;

            public Lagniappe(string lagniappe)
            {
                this.code = "";
                this.quantity = 0;

                if (lagniappe.Trim() != "")
                { 
                    string[] temp = lagniappe.Split(new Char[] { ':' });
                    this.code = temp[0];
                    if (temp.Length > 1) this.quantity = Convert.ToInt32(temp[1]);
                    else this.quantity = 1;

                    if (this.code.Trim() == "" || this.quantity < 1) throw new Exception();
                }
                else
                {
                    throw new Exception();
                }
            }
        }
        #endregion


    }

}
